<template>
  <div class="main-menu">
    <div class="logo">
      <img src="../../../public/favicon.ico" alt="" />
      <span v-show="!isFold">CMS管理系统</span>
    </div>
    <el-scrollbar style="height: 100%">
      <el-menu
        text-color="#b7bdc3"
        active-text-color="#fff"
        background-color="#001529"
        :default-active="defaultActive"
        class="el-menu-vertical-demo"
        :collapse="isFold"
      >
        <template v-for="item in menuList" :key="item.path">
          <el-sub-menu :index="item.meta.id">
            <template #title>
              <el-icon>
                <!-- 动态组件item.meta.icon=>Monitor字符串，使用动态组件处理即可 -->
                <component :is="item.meta.icon"></component>
              </el-icon>
              <span>{{ item.meta.menuItemName }}</span>
            </template>
            <template v-if="item.children">
              <template v-for="child in item.children" :key="item.meta.id">
                <el-menu-item
                  v-if="!child.redirect"
                  :index="child.meta.id"
                  @click="handleMenuItemClick(item.path, child.path)"
                  >{{ child.meta.menuItemName }}</el-menu-item
                >
              </template>
            </template>
          </el-sub-menu>
        </template>
      </el-menu></el-scrollbar
    >
  </div>
</template>

<script setup lang="ts">
import { computed, ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()
let props = defineProps({
  menuList: {
    required: true
  },
  isFold: {
    required: true,
    type: Boolean,
    default: false
  }
})

const MAIN = '/main'
// 这里格式是统一的，默认第一个子数组都是重定向因此可以直接使用第二个
// const defaultActive = ref(props.menuList[0].children[1].meta.id)
// 动态根据路由保持当前激活项，保证刷新不丢失
const defaultActive = computed(() => {
  let curRoute = route.path //当前路径
  for (let item of props.menuList) {
    // 找出有children项
    if (item.children) {
      for (let child of item.children) {
        // 不包含重定向
        if (!child.redirect && curRoute.includes(child.path)) {
          // 匹配当前路由id
          return child.meta.id
        }
      }
    }
  }
})

const handleMenuItemClick = (fatP: string, chilP: string) => {
  router.push({
    path: `${MAIN}/${fatP}/${chilP}`
  })
}
</script>

<style lang="scss" scoped>
.main-menu {
  position: relative;
  top: 0;
  left: 0;
  height: 100%;
  background-color: #001529;
  overflow: hidden;
  .el-scrollbar__wrap {
    overflow-x: hidden;
  }
}

.logo {
  /* 样式出了点bug，才用了定位 */
  z-index: 99;
  position: absolute;
  display: flex;
  align-items: center;
  overflow: hidden;
  height: 28px;
  padding: 12px 10px 8px 14px;
  img {
    height: 18px;
    margin: 0 10px;
  }
  span {
    font-size: 16px;
    font-weight: 700;
    color: white;
    white-space: nowrap;
  }
}
.el-menu-vertical-demo {
  padding-top: 28px;
}
.el-sub-menu {
  .el-menu-item {
    padding-left: 50px !important;
    background-color: #0c2135;
  }

  .el-menu-item:hover {
    color: #fff;
  }

  .el-menu-item.is-active {
    background-color: #0a60bd;
  }
}

:deep(.el-menu) {
  border: none;
}
</style>
